package com.brainhealth.customer.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.PatternPool;
import cn.hutool.core.util.ReUtil;
import com.brainhealth.common.core.domain.entity.SysDictData;
import com.brainhealth.common.enums.ShowStatus;
import com.brainhealth.common.enums.LearnType;
import com.brainhealth.common.utils.DateUtils;
import com.brainhealth.customer.domain.Customer;
import com.brainhealth.customer.domain.CustomerCollect;
import com.brainhealth.customer.domain.LearnCategory;
import com.brainhealth.customer.mapper.CustomerCollectMapper;
import com.brainhealth.customer.mapper.LearnCategoryMapper;
import com.brainhealth.system.mapper.SysDictDataMapper;
import com.brainhealth.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.brainhealth.customer.mapper.LearnMapper;
import com.brainhealth.customer.domain.Learn;
import com.brainhealth.customer.service.ILearnService;

import javax.servlet.http.HttpServletRequest;

/**
 * 学习库Service业务层处理
 * 
 * @author wolong
 * @date 2023-04-23
 */
@Service
public class LearnServiceImpl implements ILearnService
{
    @Autowired
    private LearnMapper learnMapper;
    @Autowired
    private LearnCategoryMapper learnCategoryMapper;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;
    @Autowired
    private CustomerCollectMapper customerCollectMapper;

    /**
     * 查询学习库
     *
     * @param learnId 学习库主键
     * @return 学习库
     */
    @Override
    public Learn selectLearnByLearnId(Long learnId)
    {
        Learn learn = learnMapper.selectLearnByLearnId(learnId);
        LearnCategory learnCategory = learnCategoryMapper.selectLearnCategoryByLearnCategoryId(learn.getLearnCategoryId());
        learn.getLstLearnCategory().add(learnCategory);
        return learn;
    }

    @Override
    public Learn getCustomerLearn(HttpServletRequest request, Long learnId)
    {
        Customer customer = CommonUtils.getCustomerByRequest(request);
        Learn learn = learnMapper.selectLearnByLearnId(learnId);
        CustomerCollect query = new CustomerCollect();
        query.setCustomerId(customer.getCustomerId());
        query.setLearnId(learn.getLearnId());
        List<CustomerCollect> lstCustomerCollect = customerCollectMapper.selectCustomerCollectList(query);
        if (lstCustomerCollect.size() > 0)
        {
            learn.setCollect(true);
        } else
        {
            learn.setCollect(false);
        }
        return learn;
    }

    /**
     * 查询学习库列表
     *
     * @param learn 学习库
     * @return 学习库
     */
    @Override
    public List<Learn> selectLearnList(Learn learn)
    {
        List<Learn> lstData = learnMapper.selectLearnList(learn);
        LearnCategory queryLearnCategory = new LearnCategory();
        List<LearnCategory> lstAllCategory = learnCategoryMapper.selectLearnCategoryList(queryLearnCategory);
        List<SysDictData> lstLearnType = sysDictDataMapper.selectDictDataByType("learn_type");
        for (Learn data : lstData)
        {
            LearnCategory nodeCategory = lstAllCategory.stream().filter(item -> item.getLearnCategoryId() == data.getLearnCategoryId()).findFirst().orElse(null);
            List<LearnCategory> lstCategory = new ArrayList<>();
            getLearnAllCategory(nodeCategory, lstAllCategory, lstCategory);
            if (lstCategory.size() > 0)
            {
                lstCategory.add(nodeCategory);
                data.getLstLearnCategory().addAll(lstCategory);
            }
            SysDictData learnTypeValue = lstLearnType.stream().filter(item -> item.getDictValue().equals(data.getLearnType().toString())).findFirst().orElse(null);
            data.setLearnTypeName(learnTypeValue.getDictLabel());
            if (data.getStatus() == ShowStatus.UpShelves)
            {
                data.setStatusName(ShowStatus.UpShelves.getInfo());
            }
            if (data.getStatus() == ShowStatus.DownShelves)
            {
                data.setStatusName(ShowStatus.DownShelves.getInfo());
            }
        }
        return lstData;
    }

    /**
     * 根据学习库的当前分类获取所有的父级分类
     *
     * @param node
     * @param lstAllCategory
     * @param lstCategory
     */
    public void getLearnAllCategory(LearnCategory node, List<LearnCategory> lstAllCategory, List<LearnCategory> lstCategory)
    {
        LearnCategory parentNode = lstAllCategory.stream().filter(item -> item.getLearnCategoryId() == node.getParentId()).findFirst().orElse(null);
        if (parentNode != null)
        {
            lstCategory.add(parentNode);
            getLearnAllCategory(parentNode, lstAllCategory, lstCategory);
        }
    }

    /**
     * 新增学习库
     *
     * @param learn 学习库
     * @return 结果
     */
    @Override
    public int insertLearn(Learn learn)
    {
        learn.setCreateTime(DateUtils.getNowDate());
        return learnMapper.insertLearn(learn);
    }

    /**
     * 修改学习库
     *
     * @param learn 学习库
     * @return 结果
     */
    @Override
    public int updateLearn(Learn learn)
    {
        learn.setUpdateTime(DateUtils.getNowDate());
        return learnMapper.updateLearn(learn);
    }

    /**
     * 批量删除学习库
     *
     * @param learnIds 需要删除的学习库主键
     * @return 结果
     */
    @Override
    public int deleteLearnByLearnIds(Long[] learnIds)
    {
        return learnMapper.deleteLearnByLearnIds(learnIds);
    }

    /**
     * 删除学习库信息
     *
     * @param learnId 学习库主键
     * @return 结果
     */
    @Override
    public int deleteLearnByLearnId(Long learnId)
    {
        return learnMapper.deleteLearnByLearnId(learnId);
    }

    @Override
    public Learn nextLearnVideo(Long learnId)
    {
        Learn queryLearn = new Learn();
        List<Learn> lstLearn = learnMapper.selectLearnList(queryLearn).stream().filter(item -> item.getLearnType() == LearnType.LearnVideo).collect(Collectors.toList());
        int pos = 0;
        for (int i = 0; i < lstLearn.size(); i++)
        {
            Learn learn = lstLearn.get(i);
            if (learn.getLearnId().equals(learnId))
            {
                pos = i;
                break;
            }
        }
        if (pos == lstLearn.size() - 1)
        {
            return null;
        }
        return lstLearn.get(pos + 1);
    }
}
